﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SP.SPProjects.WebParts.Presentation;
using SP.SPProjects.WebParts.Model;
using System.Collections.Generic;
using SP.SPProjects.WebParts.Support;
using Microsoft.SharePoint;

namespace SP.SPProjects.WebParts.WebParts.ListView
{
    public partial class ListViewUserControl : UserControl, IListTasksView
    {

        #region Fields

        ListTasksPresenter _presenter;

        #endregion

        #region Properties

        public double ElapsedTime { get; set; }

        public string TaskList { get; set; }

        public string TaskListName { get; set; }

        public IEnumerable<TasksBaseTask> UserTasks { get; set; }

        public int TaskId { get; set; }

        #endregion

        #region form events

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ListTasksPresenter(this);
            if (IsPostBack) return;
            _presenter.HandleLoad();
           
        }

        protected void StartPauseCounter(object sender, EventArgs e)
        {
            TaskId = int.Parse(((ImageButton)sender).CommandArgument);
            _presenter.HandleStartStopCounter();
        }

        protected void gvUserTasks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TasksBaseTask task = e.Row.DataItem as TasksBaseTask;

                ImageButton img = e.Row.Cells[5].FindControl("imgTime") as ImageButton;
                img.CommandArgument = task.Id.ToString();

                if (!task.TaskRunning.HasValue || !task.TaskRunning.Value)
                {
                    img.ImageUrl = Constants.IMAGE_PLAY;
                    img.ToolTip = "Start time counter";
                }
                else
                {
                    img.ImageUrl = Constants.IMAGE_STOP;
                    img.ToolTip = "Pause time counter";
                }

                HyperLink lnk = e.Row.Cells[0].FindControl("lnkTask") as HyperLink;
                lnk.NavigateUrl = "javascript:openDialogAutoSize('" + SPContext.Current.Web.Url + SPContext.Current.Web.Lists[Constants.TASK_LIST].DefaultEditFormUrl + "?ID=" + task.Id + "')";

            }
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvUserTasks.DataSource = UserTasks;
            gvUserTasks.DataBind();
        }

        #endregion

    }
}
